home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectPlay / SimpleServer / frmServer.frm next >
Text File  |  2001-10-08  |  15KB  |  415 lines

  1. VERSION 5.00
  2. Object = "{86CF1D34-0C5F-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCT2.OCX"
  3. Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
  4. Begin VB.Form frmServer 
  5.    BorderStyle     =   3  'Fixed Dialog
  6.    Caption         =   "DirectPlay Simple Server"
  7.    ClientHeight    =   4875
  8.    ClientLeft      =   45
  9.    ClientTop       =   330
  10.    ClientWidth     =   3660
  11.    Icon            =   "frmServer.frx":0000
  12.    LinkTopic       =   "Form1"
  13.    MaxButton       =   0   'False
  14.    MinButton       =   0   'False
  15.    ScaleHeight     =   4875
  16.    ScaleWidth      =   3660
  17.    StartUpPosition =   3  'Windows Default
  18.    Begin VB.CommandButton cmdStartServer 
  19.       Caption         =   "Start Server"
  20.       Default         =   -1  'True
  21.       Height          =   375
  22.       Left            =   1283
  23.       TabIndex        =   9
  24.       Top             =   4080
  25.       Width           =   1095
  26.    End
  27.    Begin VB.ListBox lstUser 
  28.       Height          =   1815
  29.       Left            =   120
  30.       TabIndex        =   8
  31.       Top             =   2160
  32.       Width           =   3375
  33.    End
  34.    Begin MSComctlLib.StatusBar sBar 
  35.       Align           =   2  'Align Bottom
  36.       Height          =   375
  37.       Left            =   0
  38.       TabIndex        =   7
  39.       Top             =   4500
  40.       Width           =   3660
  41.       _ExtentX        =   6456
  42.       _ExtentY        =   661
  43.       Style           =   1
  44.       _Version        =   393216
  45.       BeginProperty Panels {8E3867A5-8586-11D1-B16A-00C0F0283628} 
  46.          NumPanels       =   1
  47.          BeginProperty Panel1 {8E3867AB-8586-11D1-B16A-00C0F0283628} 
  48.          EndProperty
  49.       EndProperty
  50.    End
  51.    Begin MSComCtl2.UpDown udUsers 
  52.       Height          =   315
  53.       Left            =   3180
  54.       TabIndex        =   5
  55.       Top             =   1740
  56.       Width           =   240
  57.       _ExtentX        =   423
  58.       _ExtentY        =   556
  59.       _Version        =   393216
  60.       Value           =   50
  61.       BuddyControl    =   "txtUsers"
  62.       BuddyDispid     =   196611
  63.       OrigLeft        =   1800
  64.       OrigTop         =   660
  65.       OrigRight       =   2040
  66.       OrigBottom      =   975
  67.       Max             =   1000
  68.       Min             =   1
  69.       SyncBuddy       =   -1  'True
  70.       BuddyProperty   =   65547
  71.       Enabled         =   -1  'True
  72.    End
  73.    Begin VB.TextBox txtUsers 
  74.       Height          =   315
  75.       Left            =   2760
  76.       Locked          =   -1  'True
  77.       TabIndex        =   4
  78.       Text            =   "50"
  79.       Top             =   1740
  80.       Width           =   435
  81.    End
  82.    Begin VB.TextBox txtSession 
  83.       Height          =   315
  84.       Left            =   120
  85.       TabIndex        =   3
  86.       Text            =   "vbDirectPlaySession"
  87.       Top             =   1320
  88.       Width           =   3315
  89.    End
  90.    Begin VB.ListBox lstSP 
  91.       Height          =   645
  92.       Left            =   120
  93.       TabIndex        =   1
  94.       Top             =   420
  95.       Width           =   3375
  96.    End
  97.    Begin VB.Label lbl 
  98.       BackStyle       =   0  'Transparent
  99.       Caption         =   "Select the server's service provider"
  100.       Height          =   195
  101.       Index           =   2
  102.       Left            =   120
  103.       TabIndex        =   6
  104.       Top             =   120
  105.       Width           =   3435
  106.    End
  107.    Begin VB.Label lbl 
  108.       BackStyle       =   0  'Transparent
  109.       Caption         =   "Session Name"
  110.       Height          =   195
  111.       Index           =   1
  112.       Left            =   120
  113.       TabIndex        =   2
  114.       Top             =   1080
  115.       Width           =   1275
  116.    End
  117.    Begin VB.Label lbl 
  118.       BackStyle       =   0  'Transparent
  119.       Caption         =   "Maximum users:"
  120.       Height          =   255
  121.       Index           =   0
  122.       Left            =   240
  123.       TabIndex        =   0
  124.       Top             =   1800
  125.       Width           =   2415
  126.    End
  127.    Begin VB.Menu mnuPop 
  128.       Caption         =   "PopUp"
  129.       Visible         =   0   'False
  130.       Begin VB.Menu mnuShow 
  131.          Caption         =   "Show"
  132.       End
  133.       Begin VB.Menu mnuStart 
  134.          Caption         =   "Start Server"
  135.       End
  136.       Begin VB.Menu mnuSep 
  137.          Caption         =   "-"
  138.       End
  139.       Begin VB.Menu mnuExit 
  140.          Caption         =   "Exit"
  141.       End
  142.    End
  143. End
  144. Attribute VB_Name = "frmServer"
  145. Attribute VB_GlobalNameSpace = False
  146. Attribute VB_Creatable = False
  147. Attribute VB_PredeclaredId = True
  148. Attribute VB_Exposed = False
  149. Option Explicit
  150. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  151. '
  152. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  153. '
  154. '  File:       frmServer.frm
  155. '
  156. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  157. Implements DirectPlay8Event
  158. Private mfExit As Boolean
  159. Private Enum MsgTypes
  160.     Msg_NoOtherPlayers
  161.     Msg_NumPlayers
  162.     Msg_SendWave
  163. End Enum
  164.  
  165. Private Sub cmdStartServer_Click()
  166.     Dim AppDesc As DPN_APPLICATION_DESC
  167.     
  168.     If gfStarted Then Exit Sub
  169.     If Val(txtUsers.Text) < 1 Then
  170.         MsgBox "I'm sorry, you must allow at least 1 user to join your server.", vbOKOnly Or vbInformation, "Increase users"
  171.         Exit Sub
  172.     End If
  173.     
  174.     If txtSession.Text = vbNullString Then
  175.         MsgBox "I'm sorry, you must enter a session name.", vbOKOnly Or vbInformation, "No session name"
  176.         Exit Sub
  177.     End If
  178.     
  179.     'Save our current session name for later runs
  180.     SaveSetting "VBDirectPlay", "Defaults", "ServerGameName", txtSession.Text
  181.     
  182.     'Now set up the app description
  183.     With AppDesc
  184.         .guidApplication = AppGuid
  185.         .lMaxPlayers = Val(txtUsers.Text)
  186.         .SessionName = txtSession.Text
  187.         .lFlags = DPNSESSION_CLIENT_SERVER 'We must pass the client server flags if we are a server
  188.     End With
  189.     
  190.     'Now set up our address value
  191.     dpa.SetSP dps.GetServiceProvider(lstSP.ListIndex + 1).Guid
  192.     
  193.     'Now start the server
  194.     dps.Host AppDesc, dpa
  195.     
  196.     gfStarted = True
  197.     sBar.SimpleText = "Server running...  (" & CStr(glNumPlayers) & "/" & txtUsers.Text & " clients connected.)"
  198.     
  199.     'modify our icon text
  200.     sysIcon.sTip = "Server running...  (" & CStr(glNumPlayers) & "/" & txtUsers.Text & " clients connected.)" & vbNullChar
  201.     sysIcon.uFlags = NIF_TIP
  202.     Shell_NotifyIcon NIM_MODIFY, sysIcon
  203.     
  204.     cmdStartServer.Enabled = False
  205. End Sub
  206.  
  207. Private Sub Form_Load()
  208.     Dim lCount As Long
  209.     Dim dpn As DPN_SERVICE_PROVIDER_INFO
  210.     
  211.     dps.RegisterMessageHandler Me
  212.     'First load our list of Service Providers into our box
  213.     For lCount = 1 To dps.GetCountServiceProviders
  214.         dpn = dps.GetServiceProvider(lCount)
  215.         lstSP.AddItem dpn.Name
  216.         'Pick the TCP/IP connection by default
  217.         If InStr(dpn.Name, "TCP") Then lstSP.ListIndex = lstSP.ListCount - 1
  218.     Next
  219.     If lstSP.ListIndex < 0 Then lstSP.ListIndex = 0
  220.     txtSession.Text = GetSetting("VBDirectPlay", "Defaults", "ServerGameName", "vbDirectPlayServer")
  221.     sBar.SimpleText = "Server not running..."
  222.     
  223.     'Lets put an icon in the system tray
  224.     With sysIcon
  225.         .cbSize = LenB(sysIcon)
  226.         .hwnd = Me.hwnd
  227.         .uFlags = NIF_DOALL
  228.         .uCallbackMessage = WM_MOUSEMOVE
  229.         .hIcon = Me.Icon
  230.         .sTip = "vbDirectPlayServer - Server not running" & vbNullChar
  231.     End With
  232.     Shell_NotifyIcon NIM_ADD, sysIcon
  233.  
  234. End Sub
  235.  
  236. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  237.     Dim ShellMsg As Long
  238.     
  239.     ShellMsg = X / Screen.TwipsPerPixelX
  240.     Select Case ShellMsg
  241.     Case WM_LBUTTONDBLCLK
  242.         mnuShow_Click
  243.     Case WM_RBUTTONUP
  244.         'Show the menu
  245.         If gfStarted Then mnuStart.Enabled = False
  246.         PopupMenu mnuPop, , , , mnuShow
  247.     End Select
  248.     
  249. End Sub
  250.  
  251. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  252.     If Not mfExit Then
  253.         Cancel = 1
  254.         Me.Hide
  255.     End If
  256. End Sub
  257.  
  258. Private Sub Form_Unload(Cancel As Integer)
  259.     Shell_NotifyIcon NIM_DELETE, sysIcon
  260.     Cleanup
  261. End Sub
  262.  
  263. Private Sub mnuExit_Click()
  264.     mfExit = True
  265.     Unload Me
  266. End Sub
  267.  
  268. Private Sub mnuShow_Click()
  269.     Me.Visible = True
  270.     Me.SetFocus
  271. End Sub
  272.  
  273. Private Sub mnuStart_Click()
  274.     cmdStartServer_Click
  275. End Sub
  276.  
  277. Private Sub udUsers_Change()
  278.     Dim AppDesc As DPN_APPLICATION_DESC
  279.     
  280.     If gfStarted Then
  281.         'We need to reset our max users
  282.         AppDesc = dps.GetApplicationDesc(0)
  283.         AppDesc.lMaxPlayers = udUsers.Value
  284.         dps.SetApplicationDesc AppDesc, 0
  285.         sBar.SimpleText = "Server running...  (" & CStr(glNumPlayers) & "/" & txtUsers.Text & " clients connected.)"
  286.         
  287.         'modify our icon text
  288.         sysIcon.sTip = "Server running...  (" & CStr(glNumPlayers) & "/" & txtUsers.Text & " clients connected.)" & vbNullChar
  289.         sysIcon.uFlags = NIF_TIP
  290.         Shell_NotifyIcon NIM_MODIFY, sysIcon
  291.         NotifyEveryoneOfNumPlayers
  292.     End If
  293. End Sub
  294.  
  295. Private Sub NotifyEveryoneOfNumPlayers()
  296.     Dim oBuf() As Byte
  297.     Dim lMsg As Long, lOffset As Long
  298.     
  299.     'Here we will notify everyone currently in the session about the number of players in the session
  300.     lOffset = NewBuffer(oBuf)
  301.     lMsg = Msg_NumPlayers
  302.     AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  303.     AddDataToBuffer oBuf, glNumPlayers, LenB(glNumPlayers), lOffset
  304.     AddDataToBuffer oBuf, CLng(udUsers.Value), SIZE_LONG, lOffset
  305.     dps.SendTo DPNID_ALL_PLAYERS_GROUP, oBuf, 0, DPNSEND_NOLOOPBACK
  306. End Sub
  307.  
  308. Private Sub DirectPlay8Event_AddRemovePlayerGroup(ByVal lMsgID As Long, ByVal lPlayerID As Long, ByVal lGroupID As Long, fRejectMsg As Boolean)
  309.     'VB requires that we must implement *every* member of this interface
  310. End Sub
  311.  
  312. Private Sub DirectPlay8Event_AppDesc(fRejectMsg As Boolean)
  313.     'VB requires that we must implement *every* member of this interface
  314. End Sub
  315.  
  316. Private Sub DirectPlay8Event_AsyncOpComplete(dpnotify As DxVBLibA.DPNMSG_ASYNC_OP_COMPLETE, fRejectMsg As Boolean)
  317.     'VB requires that we must implement *every* member of this interface
  318. End Sub
  319.  
  320. Private Sub DirectPlay8Event_ConnectComplete(dpnotify As DxVBLibA.DPNMSG_CONNECT_COMPLETE, fRejectMsg As Boolean)
  321.     'VB requires that we must implement *every* member of this interface
  322. End Sub
  323.  
  324. Private Sub DirectPlay8Event_CreateGroup(ByVal lGroupID As Long, ByVal lOwnerID As Long, fRejectMsg As Boolean)
  325.     'VB requires that we must implement *every* member of this interface
  326. End Sub
  327.  
  328. Private Sub DirectPlay8Event_CreatePlayer(ByVal lPlayerID As Long, fRejectMsg As Boolean)
  329.     On Error Resume Next
  330.     Dim dpPeer As DPN_PLAYER_INFO
  331.     dpPeer = dps.GetClientInfo(lPlayerID)
  332.     If Err Then Exit Sub
  333.     glNumPlayers = glNumPlayers + 1
  334.     sBar.SimpleText = "Server running...  (" & CStr(glNumPlayers) & "/" & txtUsers.Text & " clients connected.)"
  335.     sysIcon.sTip = "Server running...  (" & CStr(glNumPlayers) & "/" & txtUsers.Text & " clients connected.)" & vbNullChar
  336.     sysIcon.uFlags = NIF_TIP
  337.     Shell_NotifyIcon NIM_MODIFY, sysIcon
  338.     'Add this player to the list
  339.     lstUser.AddItem dpPeer.Name & " DPlay ID: 0x" & Hex$(lPlayerID)
  340.     lstUser.ItemData(lstUser.ListCount - 1) = lPlayerID
  341.     NotifyEveryoneOfNumPlayers
  342. End Sub
  343.  
  344. Private Sub DirectPlay8Event_DestroyGroup(ByVal lGroupID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  345.     'VB requires that we must implement *every* member of this interface
  346. End Sub
  347.  
  348. Private Sub DirectPlay8Event_DestroyPlayer(ByVal lPlayerID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  349.     Dim lCount As Long
  350.     For lCount = lstUser.ListCount - 1 To 0 Step -1
  351.         If lstUser.ItemData(lCount) = lPlayerID Then 'remove this player from the list
  352.             lstUser.RemoveItem lCount
  353.         End If
  354.     Next
  355.     glNumPlayers = glNumPlayers - 1
  356.     sBar.SimpleText = "Server running...  (" & CStr(glNumPlayers) & "/" & txtUsers.Text & " clients connected.)"
  357.     sysIcon.sTip = "Server running...  (" & CStr(glNumPlayers) & "/" & txtUsers.Text & " clients connected.)" & vbNullChar
  358.     sysIcon.uFlags = NIF_TIP
  359.     Shell_NotifyIcon NIM_MODIFY, sysIcon
  360.     NotifyEveryoneOfNumPlayers
  361. End Sub
  362.  
  363. Private Sub DirectPlay8Event_EnumHostsQuery(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_QUERY, fRejectMsg As Boolean)
  364.     'VB requires that we must implement *every* member of this interface
  365. End Sub
  366.  
  367. Private Sub DirectPlay8Event_EnumHostsResponse(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_RESPONSE, fRejectMsg As Boolean)
  368.     'VB requires that we must implement *every* member of this interface
  369. End Sub
  370.  
  371. Private Sub DirectPlay8Event_HostMigrate(ByVal lNewHostID As Long, fRejectMsg As Boolean)
  372.     'VB requires that we must implement *every* member of this interface
  373. End Sub
  374.  
  375. Private Sub DirectPlay8Event_IndicateConnect(dpnotify As DxVBLibA.DPNMSG_INDICATE_CONNECT, fRejectMsg As Boolean)
  376.     'VB requires that we must implement *every* member of this interface
  377. End Sub
  378.  
  379. Private Sub DirectPlay8Event_IndicatedConnectAborted(fRejectMsg As Boolean)
  380.     'VB requires that we must implement *every* member of this interface
  381. End Sub
  382.  
  383. Private Sub DirectPlay8Event_InfoNotify(ByVal lMsgID As Long, ByVal lNotifyID As Long, fRejectMsg As Boolean)
  384.     'VB requires that we must implement *every* member of this interface
  385. End Sub
  386.  
  387. Private Sub DirectPlay8Event_Receive(dpnotify As DxVBLibA.DPNMSG_RECEIVE, fRejectMsg As Boolean)
  388.     Dim oNewMsg() As Byte, lOffset As Long
  389.     Dim lMsg As Long
  390.     
  391.     'The only message we will receive from our client is one to make faces to everyone
  392.     'else on the server, if there is someone else to make faces at, do it, otherwise let
  393.     'them know
  394.     If glNumPlayers > 1 Then
  395.         lOffset = NewBuffer(oNewMsg)
  396.         lMsg = Msg_SendWave
  397.         AddDataToBuffer oNewMsg, lMsg, LenB(lMsg), lOffset
  398.         AddStringToBuffer oNewMsg, dps.GetClientInfo(dpnotify.idSender).Name, lOffset
  399.         dps.SendTo DPNID_ALL_PLAYERS_GROUP, oNewMsg, 0, DPNSEND_NOLOOPBACK
  400.     Else
  401.         lOffset = NewBuffer(oNewMsg)
  402.         lMsg = Msg_NoOtherPlayers
  403.         AddDataToBuffer oNewMsg, lMsg, LenB(lMsg), lOffset
  404.         dps.SendTo DPNID_ALL_PLAYERS_GROUP, oNewMsg, 0, DPNSEND_NOLOOPBACK
  405.     End If
  406. End Sub
  407.  
  408. Private Sub DirectPlay8Event_SendComplete(dpnotify As DxVBLibA.DPNMSG_SEND_COMPLETE, fRejectMsg As Boolean)
  409.     'VB requires that we must implement *every* member of this interface
  410. End Sub
  411.  
  412. Private Sub DirectPlay8Event_TerminateSession(dpnotify As DxVBLibA.DPNMSG_TERMINATE_SESSION, fRejectMsg As Boolean)
  413.     'VB requires that we must implement *every* member of this interface
  414. End Sub
  415.